﻿using UnityEngine;
using Anderson.PluginFramework;
using System.Collections.Generic;
using Anderson.Utils;

namespace Sample
{
    public class FrameworkFactory : MonoBehaviour, IFrameworkFactory
    {
        #region IRotateApp
        public Transform RotateObject;
        public bool AutoStart = true;
        #endregion

        #region IFrameProperties
        public Transform PluginRootTransform;
        public AndersonLog log;

        IFramework m_framework = null;
        public IFramework Framework
        {
            get
            {
                return m_framework;
            }
        }
        #endregion

        public void OnFrameworkLoad()
        {
            if (null == m_framework)
            {
                PluginEntity entity = new PluginEntity();
                entity.id = 1;
                entity.pluginAssetType = 1;
                entity.pluginName = "SamplePlugin";
                entity.projectPath = "assets/plugins/sampleplugin.bytes";
                //Win10需要增加前缀file:///
                entity.assetPathOrUrl = "file:///D:/Temp/Anderson/PluginAssets/sample.plugin";

                PluginEntity entity2 = new PluginEntity();
                entity2.id = 2;
                entity2.pluginAssetType = 1;
                entity2.pluginName = "GameObjectPlugin";
                entity2.projectPath = "assets/plugins/gameobjectplugin.bytes";
                entity2.assetPathOrUrl = "file:///D:/Temp/Anderson/PluginAssets/gameobject.plugin";

                PluginEntity entity3 = new PluginEntity();
                entity3.id = 3;
                entity3.pluginAssetType = 1;
                entity3.pluginName = "ListenerPlugin";
                entity3.projectPath = "assets/plugins/listenerplugin.bytes";
                entity3.assetPathOrUrl = "file:///D:/Temp/Anderson/PluginAssets/listener.plugin";

                PluginEntity entity4 = new PluginEntity();
                entity4.id = 4;
                entity4.pluginAssetType = 1;
                entity4.pluginName = "Service";
                entity4.projectPath = "assets/plugins/RotateService.bytes";
                entity4.assetPathOrUrl = "file:///D:/Temp/Anderson/PluginAssets/rotate.service";

                PluginEntity entity5 = new PluginEntity();
                entity5.id = 5;
                entity5.pluginAssetType = 1;
                entity5.pluginName = "UseRotateServicePlugin";
                entity5.projectPath = "assets/plugins/ServicePlugin.bytes";
                entity5.assetPathOrUrl = "file:///D:/Temp/Anderson/PluginAssets/userotateservice.plugin";

                IList<PluginEntity> rootEntities = new List<PluginEntity>();
                //rootEntities.Add(entity);
                //rootEntities.Add(entity2);
                //rootEntities.Add(entity3);
                rootEntities.Add(entity4);
                rootEntities.Add(entity5);

                m_framework = gameObject.AddComponent<RotateApp>();
                m_framework.Log = log;
                m_framework.RootEntities = rootEntities;
                m_framework.PluginRootTransform = PluginRootTransform;
                m_framework.AutoStartBeforeLoaded = AutoStart;

                IRotateApp app = (RotateApp)m_framework;
                app.RotateObject = RotateObject;
            }

            if (!m_framework.Resolved)
            {
                m_framework.Load();
            }
        }

        public void OnFrameworkRun()
        {
            if (null != m_framework && m_framework.Resolved && !m_framework.Started)
            {
                m_framework.Run();
            }
        }

        public void OnFrameworkStop()
        {
            if (null != m_framework && m_framework.Resolved && m_framework.Started)
            {
                m_framework.Stop();
            }
        }

        public void OnFrameworkUnInstall()
        {
            if (null != m_framework && m_framework.Resolved)
            {
                m_framework.Uninstall();
            }
        }

    }
}